package com.stylefeng.guns.modular.netcar.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.core.validater.Insert;
import com.stylefeng.guns.core.validater.Update;
import com.stylefeng.guns.modular.netcar.model.TaskInf;
import com.stylefeng.guns.modular.netcar.service.ITaskInfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
/**
 * 任务单信息
 *
 * @author sasazaixian
 * @Date 2018-10-25 18:14
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/taskInf"})
@Api(value = "任务单信息操作相关api", tags = {"任务单信息操作接口"})
public class TaskInfController extends BaseController {
    @Autowired
    private ITaskInfService taskInfService;
	
    /**
     * 分页获取任务单信息列表
     * @param taskInf
     * @param pageNum
     * @param pageSize
     * @return	
     */
    @ApiOperation(value = "分页获取任务单信息列表", notes = "分页获取任务单信息列表", httpMethod = "POST")
    @PostMapping(value = "/pageList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result pageList(
        @RequestBody @ApiParam(name = "taskInf", value = "任务单信息对象")TaskInf taskInf,
        @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
        @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize		
        ) {
        try {
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            pager.setObj(taskInf);			
            return Result.success( taskInfService.pageListTaskInf(pager,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 车队指派查询
     * @param dispatchState
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "车企查询需要指派或指派过得订单", notes = "车企查询需要指派或指派过得订单", httpMethod = "POST")
    @PostMapping(value = "/pageListDispatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result pageListDispatch (
            @ApiParam(name = "dispatchState", value = "指派状态 0:未指派 1:已经指派",required=false) @RequestParam(value = "dispatchState",required = false) String dispatchState,
            @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) {
        try {
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            TaskInf taskInf=new TaskInf();
            if(dispatchState!=null){
                taskInf.setDispatchState(dispatchState);
            }
            pager.setObj(taskInf);
            return Result.success( taskInfService.pageListDispatch(pager,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 车企查询自己承运的订单
     * @param tiState
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "车企查询自己承运的订单", notes = "车队指派查询", httpMethod = "POST")
    @PostMapping(value = "/pageListDriverOrg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result pageListDriverOrg (
            @ApiParam(name = "tiState", value = "任务状态 1:未完成 2:已经",required = false) @RequestParam(value = "tiState",required = false) Integer tiState,
            @ApiParam(name = "pageNum", value = "当前页数", required = true) @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页显示条数", required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) {
        try {
            Pager pager=new Pager();
            pager.setPageNumber(pageNum);//页码从1 开始
            pager.setPageSize(pageSize);
            TaskInf taskInf=new TaskInf();
            taskInf.setTiState(""+tiState);
            pager.setObj(taskInf);
            return Result.success( taskInfService.pageListDriverOrg(pager,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取任务单信息列表
     * @param taskInf    
     * @return	
     */
    @ApiOperation(value = "获取任务单信息列表", notes = "获取任务单信息列表", httpMethod = "POST")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)	
    @ResponseBody
    public Result list(
        @RequestBody @ApiParam(name = "taskInf", value = "任务单信息对象")TaskInf taskInf) {
        try {            
            return Result.success( taskInfService.selectListTaskInf(taskInf,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 通过主键获取任务单信息对象
     * @param id
     * @return
     */
    @ApiOperation(value = "通过主键获取任务单信息对象", notes = "通过主键获取任务单信息对象", httpMethod = "GET")
    @GetMapping(value = "/getById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result getById(
            @ApiParam(name = "id", value = "主键ID", required = true) @RequestParam(value = "id", required = true) String  id
    ) {
        try {
            return Result.success( taskInfService.findTaskInfById(id,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增任务单信息数据
     *
     * @param taskInf 
     * @param result
     * @return
     */
    @ApiOperation(value = "新增任务单信息数据", notes = "新增任务单信息数据", httpMethod = "POST")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "新增任务单信息数据")
    @ResponseBody
    public Result add( @Validated(value = Insert.class) @RequestBody @ApiParam(name = "taskInf ", value = "任务单信息对象")TaskInf taskInf ,BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            taskInfService.insertTaskInf(taskInf,getToken(),true);
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 新增订单并且指派驾驶员
     * @param taskInf
     * @param result
     * @return
     */
    @ApiOperation(value = "新增订单并且指派驾驶员", notes = "新增订单并且指派驾驶员", httpMethod = "POST")
    @PostMapping(value = "/addOrderAndTask", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "新增订单并且指派驾驶员")
    @ResponseBody
    public Result addOrderAndTask( @Validated(value = Insert.class) @RequestBody @ApiParam(name = "taskInf ", value = "任务单信息对象")TaskInf taskInf ,BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            taskInfService.insertOrderAndTaskInf(taskInf,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 更新任务单信息数据
     * @param taskInf
     * @param result
     * @return
     */
    @ApiOperation(value = "更新任务单信息数据", notes = "更新任务单信息数据", httpMethod = "POST")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "更新任务单信息数据")
    @ResponseBody
    public Result update(@Validated(value = Update.class) @RequestBody @ApiParam(name = "taskInf", value = "任务单信息对象")TaskInf taskInf, BindingResult result
    ) {
        if (result.hasErrors()) {
            return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
        }
        try {
            taskInfService.updateTaskInf(taskInf,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 更新任务包中的订单信息
     * @param tiId
     * @param oiId
     * @param ope
     * @return
     */
    @ApiOperation(value = "更新任务包中的订单信息", notes = "更新任务包中的订单信息", httpMethod = "POST")
    @PostMapping(value = "/updateTaskOperOrder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result updateTaskOperOrder(
            @ApiParam(name = "tiId", value = "任务包ID", required = true) @RequestParam(value = "tiId", required = true) String  tiId,
            @ApiParam(name = "oiId", value = "oiId", required = true) @RequestParam(value = "oiId", required = true) String  oiId,
            @ApiParam(name = "ope", value = "操作 I(insert)/D(del)", required = true) @RequestParam(value = "ope", required = true) String  ope
    ) {
        if(tiId==null || tiId.trim().length()<10 || oiId==null || oiId.trim().length()<10 ||
            ope==null|| !(ope.equals("I") || ope.equals("D")) ){
            Result.error(CodeMsg.SERVER_EXCEPTION,"参数无效！");
        }
        try {
            return Result.success( taskInfService.updateTaskInfOpeOrder(tiId,oiId,ope,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 取消任务指派
     * @param tiId
     * @return
     */
    @ApiOperation(value = "取消任务指派", notes = "取消任务指派", httpMethod = "POST")
    @PostMapping(value = "/updateTaskCancelDispatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result updateTaskCancelDispatch(
            @ApiParam(name = "tiId", value = "任务包ID", required = true) @RequestParam(value = "tiId", required = true) String  tiId ) {
        if(tiId==null || tiId.trim().length()<10 ){
            Result.error(CodeMsg.SERVER_EXCEPTION,"参数无效！");
        }
        try {
            return Result.success(taskInfService.updateTaskInfCancelDispatch(tiId,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 删除任务单信息数据
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除任务单信息数据", notes = "删除任务单信息数据", httpMethod = "GET")
    @GetMapping(value = "/delBatch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除任务单信息数据")
    @ResponseBody
    public Result delBatch(
            @ApiParam(name = "ids", value = "主键ID字符数组", required = true) @RequestParam(value = "ids", required = true) String ids
    ) {
        try {
            taskInfService.deleteBatchTaskInf(ids.split(","),getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	/**
     * 删除任务单信息数据
     * @param taskInf
     * @return
     */
    @ApiOperation(value = "删除任务单信息数据", notes = "删除任务单信息数据", httpMethod = "POST")
    @PostMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @BussinessLog(value = "删除任务单信息数据")
    @ResponseBody
    public Result del(@RequestBody @ApiParam(name = "taskInf", value = "任务单信息对象")TaskInf taskInf) {
        try {
            taskInfService.deleteTaskInf(taskInf,getToken());
            return Result.success();
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
    /**
     * 获取任务单信息的Key-Value列表，用于html的Select数据源
     * @param taskInf
     * @return
     */
    @ApiOperation(value = "获取任务单信息的Key-Value列表，用于html的Select数据源", notes = "获取任务单信息的Key-Value列表，用于html的Select数据源", httpMethod = "POST")
    @PostMapping(value = "/optionsList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Result optionsList(@RequestBody @ApiParam(name = "taskInf", value = "任务单信息对象")TaskInf taskInf) {
        try {
            return Result.success(taskInfService.optionsTaskInf(taskInf,getToken()));
        }catch(Exception e){
            return Result.error(CodeMsg.SERVER_EXCEPTION,e.getMessage());
        }
    }
	private String getToken(){		
		return getHttpServletRequest().getHeader("Authorization").substring(7);
    }
}
